﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;

namespace GUI
{
    public partial class FormDSDatHang : Window
    {
        int confirm;
        public FormDSDatHang()
        {
            InitializeComponent();
            hoiThoat = false;
        }

        private void FormDSDatHang_Load(object sender, EventArgs e)
        {
            this.dtbBatDau.Value = DateTime.Now;
            this.dtbKetThuc.Value = DateTime.Now;    
            LoadPage();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 1)
            {
                FormChiTietDonDatHang frm = new FormChiTietDonDatHang();
                frm.state = 1;
                int MADH = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
                frm.MaDH = MADH;
                frm.MdiParent = this.MdiParent;                
                frm.Show();
            }

        }
        private void LoadPage()
        {
            DateTime From = dtbBatDau.Value;
            DateTime To = dtbKetThuc.Value;
            DatHangBUS DHBUS = new DatHangBUS();
            DataSet Result = DHBUS.LoadFormBUS(From, To);
            this.dataGridView1.DataSource = Result.Tables[0];
            dataGridView1.Columns[0].HeaderText = "Mã HĐ";
            dataGridView1.Columns[1].HeaderText = "Ngày đặt hàng";
            dataGridView1.Columns[2].HeaderText = "Đã nhận đủ";
             dataGridView1.Columns[3].HeaderText = "Ghi chú";
            if (Result.Tables[0].Rows.Count != 0)
            {
                for (int j = 0; j < dataGridView1.Rows.Count - 1; j++)
                {
                    string k = dataGridView1.Rows[j].Cells[1].Value.ToString();
                    k = k.Split(' ').First();
                    dataGridView1.Rows[j].Cells[1].Value = k;
                }
            }
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            LoadPage();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            FormChiTietDonDatHang frm = new FormChiTietDonDatHang();
            frm.MdiParent = this.MdiParent;
            frm.Show();
            FormDSDatHang_Load(null, null);
            LoadPage();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
                
            DatHangBUS DHBUS = new DatHangBUS();
            if (dataGridView1.Rows.Count >= 1)
            {
                FormXoaConfirm frm = new FormXoaConfirm();
                frm.ShowDialog();
                confirm = 0;
                this.confirm = frm.confirm;
                if (confirm == 1)
                {
                    int id = int.Parse(this.dataGridView1.Rows[this.dataGridView1.CurrentCellAddress.Y].Cells[0].Value.ToString());
                    if (this.dataGridView1.SelectedRows.Count > 0)
                    {
                        dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
                    }
                    int a = DHBUS.DELETECTDH(id);
                    int b = DHBUS.DELETEDH(id);
                    {
                        if (a == 1 && b == 1)
                        {
                            MessageBox.Show("Xóa Thành Công");
                            LoadPage();
                        }
                    }
                }
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            XuatExcel(dataGridView1, "DatHang");
        }


    }
}

